package com.github.wxiaoqi.security.biz.modules.merchant.rest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.wxiaoqi.security.biz.common.cms.request.FsbpCashierCounterQueryParam;
import com.github.wxiaoqi.security.biz.common.msg.BizResponseCode;
import com.github.wxiaoqi.security.biz.modules.community.biz.CommunityViewpointBiz;
import com.github.wxiaoqi.security.biz.modules.community.entity.CommunityViewpoint;
import com.github.wxiaoqi.security.biz.modules.community.rest.ViewpointRestController;
import com.github.wxiaoqi.security.biz.modules.merchant.biz.FsbpCashierCounterBiz;
import com.github.wxiaoqi.security.biz.modules.merchant.entity.FsbpCashierCounter;
import com.github.wxiaoqi.security.biz.util.SHA256Utils;
import com.github.wxiaoqi.security.common.msg.TableResultResponse;
import com.github.wxiaoqi.security.common.rest.BaseController;

import lombok.extern.slf4j.Slf4j;



/**
 * 终端配置管理
 * 
 * @author zhangguokun
 * @email zhangguokun@icloudpay.net.cn
 * @date 2017-08-25 15:30:26
 */
@Slf4j
@RestController
@RequestMapping("fsbpCashierCounter")
public class FsbpCashierCounterController extends BaseController<FsbpCashierCounterBiz,FsbpCashierCounter>{

	@Autowired
	private FsbpCashierCounterBiz fsbpCashierCounterBiz;


	/**
	 * 商户终端用户列表
	 */
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public TableResultResponse<FsbpCashierCounter> list(@RequestBody FsbpCashierCounterQueryParam param) {
		log.info("查询商户终端用户列表开始...start------{}", param);
		log.info("请求参数：{}", param);
		return fsbpCashierCounterBiz.findPagables(param);
	}

	/**
	 * 获取counterNo
	 * 
	 * @param params
	 */
	@RequestMapping(value = "/getCounterNo", method = RequestMethod.POST)
	public Map<String, Object> getCounterNo(@RequestBody FsbpCashierCounterQueryParam param) {
		log.info("获取counterNo开始...start------{}", param);
		Map<String, Object> respMap = new HashMap<>();
		StringBuilder str = new StringBuilder();// 定义变长字符串
		Random random = new Random();
		// 随机生成数字，并添加到字符串
		for (int i = 0; i < 4; i++) {
			str.append(random.nextInt(10));
		}
		String merNo = param.getMerNo();
		String counterNo = null;
		if (!StringUtils.isEmpty(merNo)) {
			counterNo = merNo + str.toString();
			respMap.put("counterNo", counterNo);
			respMap.put("code", BizResponseCode.OK.getCode());
			respMap.put("message", BizResponseCode.OK.getMessage());
			respMap.put("success", true);
		} else {
			respMap.put("code", BizResponseCode.INVALID_FIELDS.getCode());
			respMap.put("message", BizResponseCode.INVALID_FIELDS.getMessage());
			respMap.put("success", false);
		}
		
		log.info("获取counterNo结束...{}",respMap);		
		return respMap;
	}

	/**
	 * 新增商户终端
	 * 
	 * @param params
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Map<String, Object> addCounterInfo(@RequestBody FsbpCashierCounterQueryParam param) {
		log.info("新增商户终端开始...start------{}", param);
		Map<String, Object> respMap = new HashMap<>();
		String merNo = param.getMerNo();
		String counterNo = param.getCounterNo();
		String nickName = param.getNickName();
		String password = param.getPassword();
		String terminalType = param.getTerminalType();
		FsbpCashierCounter FsbpCashierCounter = new FsbpCashierCounter();
		String pwd = SHA256Utils.SHA256(SHA256Utils.SHA256(password) + counterNo);
		FsbpCashierCounter.setCounterNo(counterNo);
		FsbpCashierCounter.setMerNo(merNo);
		if (!StringUtils.isEmpty(nickName)) {
			FsbpCashierCounter.setNickName(nickName);
		} else {
			respMap.put("code", BizResponseCode.FSBP_NICKNAME_IS_EMPTY.getCode());
			respMap.put("message", BizResponseCode.FSBP_NICKNAME_IS_EMPTY.getMessage());
			respMap.put("success", false);
			log.info("新增商户终端结束...{}",respMap);
			return respMap;
		}
		FsbpCashierCounter.setPassword(pwd);
		if (!StringUtils.isEmpty(terminalType)) {
			FsbpCashierCounter.setTerminalType(terminalType);
		} else {
			respMap.put("code", BizResponseCode.FSBP_TERMINALTYPE_ERROR.getCode());
			respMap.put("message", BizResponseCode.FSBP_TERMINALTYPE_ERROR.getMessage());
			respMap.put("success", false);
			log.info("新增商户终端结束...{}",respMap);
			return respMap;
		}
		fsbpCashierCounterBiz.save(FsbpCashierCounter);
		respMap.put("code", BizResponseCode.OK.getCode());
		respMap.put("message", BizResponseCode.OK.getMessage());
		respMap.put("success", true);
		log.info("新增商户终端结束...{}",respMap);
		return respMap;
	}

	/**
	 * 终端密码修改
	 * 
	 * @param context
	 */
	@RequestMapping(value = "/updatePassword", method = RequestMethod.POST)
	public Map<String, Object> updateCounterInfo(@RequestBody FsbpCashierCounterQueryParam param) {
		log.info("终端密码修改开始...start------{}", param);
		Map<String, Object> respMap = new HashMap<>();
		String counterNo = param.getCounterNo();
		Map<String, Object> reqMap = new HashMap<String, Object>();
		reqMap.put("counterNo", counterNo);
		String oldPassword = param.getOldPassword();
		String pwd = SHA256Utils.SHA256(SHA256Utils.SHA256(oldPassword) + counterNo);
		String newPassword = param.getNewPassword();
		String qnewPassword = param.getQnewPassword();

		FsbpCashierCounter FsbpCashierCounter = new FsbpCashierCounter();
		FsbpCashierCounter.setCounterNo(counterNo);
		FsbpCashierCounter.setPassword(pwd);
		List<FsbpCashierCounter> list = fsbpCashierCounterBiz.querySeletive(FsbpCashierCounter);
		if (list==null || list.size()!=1) {
			respMap.put("code", BizResponseCode.FSBP_PWD_ERROR.getCode());
			respMap.put("message", BizResponseCode.FSBP_PWD_ERROR.getMessage());
			respMap.put("success", false);
			log.info("终端密码修改结束...{}",respMap);
			return respMap;
		}
		if (!newPassword.equals(qnewPassword)) {
			respMap.put("code", BizResponseCode.FSBP_PWD_CONFIRM_ERROR.getCode());
			respMap.put("message", BizResponseCode.FSBP_PWD_CONFIRM_ERROR.getMessage());
			respMap.put("success", false);
			log.info("终端密码修改结束...{}",respMap);
			return respMap;
		}
		FsbpCashierCounter newFsbpCashierCounter=list.get(0);
		String newPwd = SHA256Utils.SHA256(SHA256Utils.SHA256(newPassword) + counterNo);
		newFsbpCashierCounter.setPassword(newPwd);
		fsbpCashierCounterBiz.update(newFsbpCashierCounter);
		respMap.put("code", BizResponseCode.OK.getCode());
		respMap.put("message", BizResponseCode.OK.getMessage());
		respMap.put("success", true);
		log.info("终端密码修改结束...{}",respMap);
		return respMap;
	}

	/**
	 * 终端密码重置
	 * 
	 * @param context
	 */
	@RequestMapping(value = "/resertPassword", method = RequestMethod.POST)
	public Map<String, Object> resertCounterInfo(@RequestBody FsbpCashierCounterQueryParam param) {
		log.info("终端密码重置开始...start------{}", param);
		Map<String, Object> respMap = new HashMap<String, Object>();
		FsbpCashierCounter entity=new FsbpCashierCounter();
		entity.setCounterNo(param.getCounterNo());
		List<FsbpCashierCounter> list = fsbpCashierCounterBiz.querySeletive(entity);
		if (list!=null && list.size()==1) {
			FsbpCashierCounter FsbpCashierCounter = list.get(0);
			String password = SHA256Utils.SHA256(SHA256Utils.SHA256("123456") + param.getCounterNo());
			FsbpCashierCounter.setPassword(password);
			fsbpCashierCounterBiz.update(FsbpCashierCounter);	
			respMap.put("code", BizResponseCode.OK.getCode());
			respMap.put("message", BizResponseCode.OK.getMessage());
			respMap.put("success", true);
		} else {
			respMap.put("code", BizResponseCode.FSBP_RESET_PWD_ERROR.getCode());
			respMap.put("message", BizResponseCode.FSBP_RESET_PWD_ERROR.getMessage());
			respMap.put("success", false);
			log.info("终端密码重置结束...{}",respMap);
		}
		log.info("终端密码重置结束...{}",respMap);
		return respMap;
	}

	/**
	 * 删除记录
	 * 
	 * @param context
	 */
	@RequestMapping(value = "/del", method = RequestMethod.POST)
	public Map<String, Object> delCounterInfo(@RequestBody FsbpCashierCounterQueryParam param) {
		log.info("删除记录开始...start------{}", param);
		Map<String, Object> respMap = new HashMap<String, Object>();
		String counterNo = param.getCounterNo();
		if (!StringUtils.isEmpty(counterNo)) {
			fsbpCashierCounterBiz.deleteBatch(counterNo);
			respMap.put("code", BizResponseCode.OK.getCode());
			respMap.put("message", BizResponseCode.OK.getMessage());
			respMap.put("success", true);
		} else {
			respMap.put("code", BizResponseCode.FSBP_DEL_ERROR.getCode());
			respMap.put("message", BizResponseCode.FSBP_DEL_ERROR.getMessage());
			respMap.put("success", false);
		}
		log.info("删除记录结束...{}",respMap);
		return respMap;
	}


}
